﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using QuanCafe_BUS_WS.Uploader;
using System.IO;

namespace QuanCafe_BUS_WS
{
    public class UploadFileImage // class này dùng để up file hình lên web http://quangcaokhacviet.somee.com/Image/
    {

        // viết hàm upload image lên thư mục nhanvien
        public bool UpLoadImageNhanVien(string filename)
        { 
            try
            {
                // lấy đường dẫn
                String strFile = System.IO.Path.GetFileName(filename);

                //gọi service up hình
                FileUploaderSoapClient srv = new FileUploaderSoapClient();

                // lấy thông tin file
                FileInfo fInfo = new FileInfo(filename);

                // kiểm tra kích thước file -> tối đa 4MB
                long numBytes = fInfo.Length;
                double dLen = Convert.ToDouble(fInfo.Length / 1000000);

                // Default limit of 4 MB on web server
                // have to change the web.config to if
                // you want to allow larger uploads
                if (dLen < 4)
                {
                    // set up a file stream and binary reader for the 
                    // selected file
                    FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fStream);

                    // convert the file to a byte array
                    byte[] data = br.ReadBytes((int)numBytes);
                    br.Close();

                    // pass the byte array (file) and file name to the web service
                    string sTmp = srv.UploadImageNhanVien(data, strFile);
                    fStream.Close();
                    fStream.Dispose();

                    // this will always say OK unless an error occurs,
                    // if an error occurs, the service returns the error message
                    return true; 
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        // viết hàm upload image lên thư mục ThucUong
        public bool UpLoadImageThucUong(string filename)
        {

            try
            {
                // lấy đường dẫn
                String strFile = System.IO.Path.GetFileName(filename);

                //gọi service up hình
                FileUploaderSoapClient srv = new FileUploaderSoapClient();

                // lấy thông tin file
                FileInfo fInfo = new FileInfo(filename);

                // kiểm tra kích thước file -> tối đa 4MB
                long numBytes = fInfo.Length;
                double dLen = Convert.ToDouble(fInfo.Length / 1000000);

                // Default limit of 4 MB on web server
                // have to change the web.config to if
                // you want to allow larger uploads
                if (dLen < 4)
                {
                    // set up a file stream and binary reader for the 
                    // selected file
                    FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fStream);

                    // convert the file to a byte array
                    byte[] data = br.ReadBytes((int)numBytes);
                    br.Close();

                    // pass the byte array (file) and file name to the web service
                    string sTmp = srv.UploadImageThucUong(data, strFile);
                    fStream.Close();
                    fStream.Dispose();

                    // this will always say OK unless an error occurs,
                    // if an error occurs, the service returns the error message
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        // viết hàm upload image lên thư mục NhomThucUong
        public bool UpLoadImageNhomThucUong(string filename)
        {

            try
            {
                // lấy đường dẫn
                String strFile = System.IO.Path.GetFileName(filename);

                //gọi service up hình
                FileUploaderSoapClient srv = new FileUploaderSoapClient();

                // lấy thông tin file
                FileInfo fInfo = new FileInfo(filename);

                // kiểm tra kích thước file -> tối đa 4MB
                long numBytes = fInfo.Length;
                double dLen = Convert.ToDouble(fInfo.Length / 1000000);

                // Default limit of 4 MB on web server
                // have to change the web.config to if
                // you want to allow larger uploads
                if (dLen < 4)
                {
                    // set up a file stream and binary reader for the 
                    // selected file
                    FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
                    BinaryReader br = new BinaryReader(fStream);

                    // convert the file to a byte array
                    byte[] data = br.ReadBytes((int)numBytes);
                    br.Close();

                    // pass the byte array (file) and file name to the web service
                    string sTmp = srv.UploadImageNhomThucUong(data, strFile);
                    fStream.Close();
                    fStream.Dispose();

                    // this will always say OK unless an error occurs,
                    // if an error occurs, the service returns the error message
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}